package zx.x.modules.app.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.mapstruct.CollectionMappingStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import zx.x.modules.app.dao.CollectionDao;
import zx.x.modules.app.entity.CollectionEntity;
import zx.x.modules.app.entity.OccupantEntity;
import zx.x.modules.app.service.CollectionService;
import zx.x.common.utils.PageUtils;
import zx.x.common.utils.R;



/**
 * 收藏酒店表
 *
 * @author x
 * @email x
 * @date 2022-10-31 16:41:17
 */
@RestController
@RequestMapping("/app/collection")
public class CollectionController {
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private CollectionDao dao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(CollectionEntity collection){
        Page page = PageHelper.startPage(collection.getPage(),collection.getLimit());
        return R.ok().put("page", collectionService.selectCollectionList(collection)).put("pages",page.getPages()).put("total",page.getTotal());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        CollectionEntity collection = collectionService.getById(id);
        return R.ok().put("collection", collection);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(CollectionEntity collection){
        QueryWrapper<CollectionEntity> queryWrapper = new QueryWrapper<CollectionEntity>();
        queryWrapper.eq("m_id",collection.getMId()).eq("user_id",collection.getUserId()).eq("delete_flag","0");
        if (collectionService.getOne(queryWrapper) != null) {
            return R.error("不可重复收藏");
        }
        collectionService.save(collection);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(CollectionEntity collection){
        collectionService.updateById(collection);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(Integer mId,Integer userId){
        QueryWrapper<CollectionEntity> queryWrapper = new QueryWrapper<CollectionEntity>();
        queryWrapper.eq("m_id",mId).eq("user_id",userId);
        dao.delete(queryWrapper);
        return R.ok();
    }

    /**
     * 判断是否收藏
     */
    @RequestMapping("/ifCollection")
    public R ifCollection(Integer mId,Integer userId){
        QueryWrapper<CollectionEntity> queryWrapper = new QueryWrapper<CollectionEntity>();
        queryWrapper.eq("m_id",mId).eq("user_id",userId).eq("delete_flag","0");
        collectionService.getOne(queryWrapper);
        if (collectionService.getOne(queryWrapper) != null) {
            return R.ok().put("flag",1);
        }
        return R.ok().put("flag",0);
    }
}
